import { getAction, postAction, putAction, deleteAction } from '@/api/manage'
import { axios } from '@/utils/request'

const queryPageList = (params) => getAction('/oms/productSalesOrder/list', params)
const getOrderDetail = (params) => getAction('/oms/productSalesOrder/detail', params)
const uploadSummaryFileAction = (params) => postAction('/oms/productSalesOrder/uploadSummaryFile', params)
const uploadOptimizationFileAction = (params) => postAction('/oms/productSalesOrder/uploadOptimizationFile', params)

// 设计文件相关
const getDesignFileByOrderId = (params) => getAction('/oms/designFile/getByOrderId', params)
const uploadSummaryFileByDesign = (params) => postAction('/oms/designFile/uploadSummaryFile', params)
const uploadOptimizationFileByDesign = (params) => postAction('/oms/designFile/uploadOptimizationFile', params)
const getRecentFiles = (params) => getAction('/oms/designFile/getRecentFiles', params)

// 统一导出API
export {
  queryPageList,
  getOrderDetail,
  uploadSummaryFileAction,
  uploadOptimizationFileAction,
  getDesignFileByOrderId,
  uploadSummaryFileByDesign,
  uploadOptimizationFileByDesign,
  getRecentFiles
}

/**
 * 获取最近的设计文件（使用axios直接调用）
 * @param {Object} params 查询参数
 * @returns {Promise} API请求Promise
 */
export function getRecentDesignFiles(params) {
  return axios({
    url: '/oms/designFile/getRecentFiles',
    method: 'get',
    params
  })
}

/**
 * 获取已审批通过的订单列表
 * @param {Object} params 查询参数
 * @returns {Promise} API请求Promise
 */
export function getApprovedOrderList(params) {
  return axios({
    url: '/oms/productSalesOrder/approvedList',
    method: 'get',
    params
  })
}

/**
 * 上传汇总单
 * @param {FormData} formData 包含订单ID和文件的表单数据
 * @returns {Promise} API请求Promise
 */
export function uploadSummaryFile(formData) {
  return axios({
    url: '/oms/designFile/uploadSummaryFile',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 上传优化单
 * @param {FormData} formData 包含订单ID和文件的表单数据
 * @returns {Promise} API请求Promise
 */
export function uploadOptimizationFile(formData) {
  return axios({
    url: '/oms/designFile/uploadOptimizationFile',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 获取订单详情（包含明细）
 * @param {String} id 订单ID
 * @returns {Promise} API请求Promise
 */
export function getOrderDetailById(id) {
  return axios({
    url: '/oms/productSalesOrder/getOrderDetailById',
    method: 'get',
    params: { id }
  })
}

/**
 * 提交订单审核
 * @param {Object} params 包含orderId的参数对象
 * @returns {Promise} API请求Promise
 */
export function submitOrderForReview(params) {
  return axios({
    url: '/oms/productSalesOrder/submitForReview',
    method: 'post',
    data: params
  })
}

/**
 * 取消上传的文件
 * @param {Object} params 包含orderId和status的参数对象
 * @returns {Promise} API请求Promise
 */
export function cancelUploadedFiles(params) {
  // 将参数转换为URL编码形式
  const formData = new URLSearchParams();
  for (const key in params) {
    if (params.hasOwnProperty(key)) {
      formData.append(key, params[key]);
    }
  }
  
  return axios({
    url: '/oms/productSalesOrder/cancelUpload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}

/**
 * 更新订单状态
 * @param {Object} params 包含id, oldStatus, newStatus, changeReason的参数对象
 * @returns {Promise} API请求Promise
 */
export function updateOrderStatus(params) {
  return axios({
    url: '/oms/productSalesOrder/updateOrderStatus',
    method: 'post',
    data: params
  })
}